package com.littlec.sdk.asynctask;

import java.io.File;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.Message;

import com.littlec.sdk.XMPPConnectionManager;
import com.littlec.sdk.entity.CMMessage;
import com.littlec.sdk.entity.messagebody.LocationMessageBody;
import com.littlec.sdk.manager.CMMessageManager;
import com.littlec.sdk.utils.CMChatGlobalStorage;
import com.littlec.sdk.utils.CMChatListener;
import com.yinhai.constant.Contact;
import com.yinhai.constant.UrlConstant;

import android.os.AsyncTask;

/**
 * 单聊发送图标
 * @author H2
 *
 */
public class FileUploadPosAsyncTask  extends AsyncTask<File, Integer, Boolean> {
	private CMMessage message;
	private CMChatListener.CallBack callback;
	private String resultJson;

	public FileUploadPosAsyncTask(CMMessage message,
			CMChatListener.CallBack callback) {
		this.message = message;
		this.callback = callback;
	}

	protected void onPreExecute() {
		this.callback.onProgress(this.message, -1);
	}

	protected Boolean doInBackground(File[] params) {
		HttpClient httpclient = new DefaultHttpClient();
		HttpPost post3 = new HttpPost(UrlConstant.LOCAL_URI
				+ "addPhoto?phone=" + message.getUuid());
		File upfile = params[0];
		MultipartEntity entity3 = new MultipartEntity();
		try {
			entity3.addPart("oicon", new FileBody(upfile));
			post3.setEntity(entity3);
			HttpResponse rsp = httpclient.execute(post3);
			HttpEntity entity = rsp.getEntity();
			if (entity != null) {
				entity.consumeContent();
			}
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		return Boolean.valueOf(true);
	}

	protected void onProgressUpdate(Integer[] integer) {
		int progress = integer[0].intValue();
		if (progress != this.message.getProgress()) {
			this.message.setProgress(progress);
			try {
				this.callback.onProgress(this.message, progress);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	protected void onPostExecute(Boolean result) {
		if (result.booleanValue()) {
			try {
				String to = message.getTo();
				Chat chat = (Chat) CMMessageManager.getInstance().chats
						.get(to);
				XMPPConnection connection = XMPPConnectionManager
						.getInstance().getConnection();
				if (connection == null) {
					callback.onError(message, "not connected");
					return;
				}
				if (chat == null) {
					chat = XMPPConnectionManager
							.getInstance()
							.getConnection()
							.getChatManager()
							.createChat(
									message.getTo()
											+ "@"
											+ XMPPConnectionManager
													.getInstance()
													.getConnection()
													.getServiceName(), null);

					CMMessageManager.getInstance().chats.put(to, chat);
				}
				if ((connection.isConnected())
						&& (connection.isAuthenticated())) {
					try {
						XMPPConnectionManager.getInstance()
								.sendOnlinePacket();
						LocationMessageBody messageBody = (LocationMessageBody) message
								.getMessageBody();
						Message messageChat = new Message();
						messageChat.setProperty("type", Contact.POSMAP);
						messageChat.setProperty("isGroup", "0");
						messageChat.setProperty("msgModelId", message.getUuid());
						messageChat.setProperty("latitude", messageBody.getLatitude()+"");
						messageChat.setProperty("longitude",messageBody.getLongitude()+"");
						messageChat.setProperty("sendName", CMChatGlobalStorage.getInstance().getmLoginUserRealName());
						messageChat.setBody(messageBody.getAddress());
						chat.sendMessage(messageChat);
						try {
							message.setProgress(100);
						} catch (Exception e) {
							e.printStackTrace();
						}
						try {
							callback.onSuccess(message);
						} catch (Exception e) {
							e.printStackTrace();
						}
						return;
					} catch (Exception e) {
						e.printStackTrace();
						if ((e.getMessage() != null)
								&& (e.getMessage()
										.equals("recipient-unavailable"))) {
							callback.onError(message,
									"recipient-unavailable");
							return;
						}
					}

				}
			} catch (Exception e) {
				e.printStackTrace();
				this.callback.onError(this.message, "文件上传失败");
			}
		} else
			this.callback.onError(this.message, "文件上传失败");
	}
}
